package cn.iocoder.yudao.module.polling.controller.admin.pollingtask;

import cn.iocoder.yudao.module.polling.controller.admin.pollingtask.vo.*;
import cn.iocoder.yudao.module.polling.convert.pollingtask.PollingTaskConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingtask.PollingTaskDO;
import cn.iocoder.yudao.module.polling.service.pollingtask.PollingTaskService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Api(tags = "管理后台 - 巡检任务")
@RestController
@RequestMapping("/abu/polling-task")
@Validated
public class PollingTaskController {

    @Resource
    private PollingTaskService pollingTaskService;

    @PostMapping("/create")
    @ApiOperation("创建巡检任务")
    @PreAuthorize("@ss.hasPermission('abu:polling-task:create')")
    public CommonResult<Integer> createPollingTask(@Valid @RequestBody PollingTaskCreateReqVO createReqVO) {
        return success(pollingTaskService.createPollingTask(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新巡检任务")
    @PreAuthorize("@ss.hasPermission('abu:polling-task:update')")
    public CommonResult<Boolean> updatePollingTask(@Valid @RequestBody PollingTaskUpdateReqVO updateReqVO) {
        pollingTaskService.updatePollingTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除巡检任务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-task:delete')")
    public CommonResult<Boolean> deletePollingTask(@RequestParam("id") Integer id) {
        pollingTaskService.deletePollingTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得巡检任务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-task:query')")
    public CommonResult<PollingTaskRespVO> getPollingTask(@RequestParam("id") Integer id) {
        PollingTaskDO pollingTask = pollingTaskService.getPollingTask(id);
        return success(PollingTaskConvert.INSTANCE.convert(pollingTask));
    }

    @GetMapping("/list")
    @ApiOperation("获得巡检任务列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-task:query')")
    public CommonResult<List<PollingTaskRespVO>> getPollingTaskList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingTaskDO> list = pollingTaskService.getPollingTaskList(ids);
        return success(PollingTaskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得巡检任务分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-task:query')")
    public CommonResult<PageResult<PollingTaskRespVO>> getPollingTaskPage(@Valid PollingTaskPageReqVO pageVO) {
        PageResult<PollingTaskDO> pageResult = pollingTaskService.getPollingTaskPage(pageVO);
        return success(PollingTaskConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出巡检任务 Excel")
    @PreAuthorize("@ss.hasPermission('abu:polling-task:export')")
    @OperateLog(type = EXPORT)
    public void exportPollingTaskExcel(@Valid PollingTaskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PollingTaskDO> list = pollingTaskService.getPollingTaskList(exportReqVO);
        // 导出 Excel
        List<PollingTaskExcelVO> datas = PollingTaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检任务.xls", "数据", PollingTaskExcelVO.class, datas);
    }

}
